﻿using B.S.Shop.Application.Command;
using B.S.Shop.Domain;
using B.S.Shop.ErrorCode;
using B.S.Shop.Infartructrue.Interfaces;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.Shop.Application.Commandhandler
{
    public class ShopUpdateRangeCommandHandler : IRequestHandler<ShopUpdateRangeCommand, APIResult<int>>
    {
        public ShopUpdateRangeCommandHandler(IBaseRepository<ShopClass> shopRepository)
        {
            ShopRepository = shopRepository;
        }

        public IBaseRepository<ShopClass> ShopRepository { get; }

        public async Task<APIResult<int>> Handle(ShopUpdateRangeCommand request, CancellationToken cancellationToken)
        {
            var str = request.Ids.Split(',').Select(a => Convert.ToInt32(a)).ToList();
            var path = await ShopRepository.GetAll().Where(a => str.Contains(a.ShopId)).ToListAsync ();
            foreach(var item in path)
            {
                item.IsShop = true;
               
            }
            await ShopRepository.UpdateRange(path);
            return new APIResult<int>
            {
                Code = ResultCode.success,
                Mag = "",
                data = 1
            };
           
        }
    }
}
